package com.lbc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lbc.bean.dto.OrganizationDTO;
import com.lbc.bean.dto.ParentDTO;
import com.lbc.bean.dto.SchoolDTO;
import com.lbc.bean.pojo.Page;
import com.lbc.common.resp.ResponseResult;
import com.lbc.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author xyl
 * @date 2021-03-22 19:38
 */
@Api(tags = "管理员接口")
@RestController
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private UserService userService;

    @RequiresRoles("admin")
    @ApiOperation("管理员添加组织机构")
    @PostMapping("/addOrganization")
    public ResponseResult addOrganization(@RequestBody OrganizationDTO organization){
        if (ObjectUtils.isNotEmpty(userService.getUserByEmail(organization.getEmail()))) {
            return ResponseResult.error("此邮箱已存在!");
        }
        return ResponseResult.toResponse(userService.adminAddOrganization(organization));
    }
    @RequiresRoles("admin")
    @ApiOperation("管理员编辑组织机构")
    @PostMapping("/modifyOrganization")
    public ResponseResult modifyOrganization(@RequestBody OrganizationDTO organization){
        if (ObjectUtils.isNotEmpty(userService.getUserByEmail(organization.getEmail()))) {
            return ResponseResult.error("此邮箱已存在!");
        }
        return ResponseResult.toResponse(userService.adminModifyOrganization(organization));
    }
    @RequiresRoles("admin")
    @ApiOperation("管理员添加学校")
    @PostMapping("/addSchool")
    public ResponseResult addSchool(@RequestBody SchoolDTO school){
        if (ObjectUtils.isNotEmpty(userService.getUserByEmail(school.getEmail()))) {
            return ResponseResult.error("此邮箱已存在!");
        }
        return ResponseResult.toResponse(userService.adminAddSchool(school));
    }
    @RequiresRoles("admin")
    @ApiOperation("管理员编辑学校")
    @PostMapping("/modifySchool")
    public ResponseResult modifySchool(@RequestBody SchoolDTO school){
        if (ObjectUtils.isNotEmpty(userService.getUserByEmail(school.getEmail()))) {
            return ResponseResult.error("此邮箱已存在!");
        }
        return ResponseResult.toResponse(userService.adminModifySchool(school));
    }
    @RequiresRoles("admin")
    @ApiOperation("管理员编辑监护人")
    @PostMapping("/modifyParent")
    public ResponseResult modifyParent(@RequestBody ParentDTO parent){
        return ResponseResult.toResponse(userService.adminModifyParent(parent));
    }

    @RequiresRoles("admin")
    @ApiOperation("管理员删除学校 和 机构")
    @DeleteMapping("/remove/{userId}")
    public ResponseResult adminRemove(@PathVariable("userId") Integer userId){
        return ResponseResult.toResponse(userService.adminRemoveUser(userId));
    }

    @RequiresRoles("admin")
    @ApiOperation("管理员审核用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户Id",dataType = "Integer",paramType = "path",required = true),
            @ApiImplicitParam(name = "state",value = "状态,1.正常 2.停用",dataType = "Integer",paramType = "query",required = true)
    })
    @PostMapping("/auditUser/{userId}")
    public ResponseResult auditUser(@PathVariable("userId") Integer userId,@RequestParam("state") Integer state){
        return ResponseResult.toResponse(userService.modifyUserState(userId,state));
    }


    @RequiresRoles("admin")
    @ApiOperation("管理员获取所有用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "第几页", dataType = "Integer", paramType = "query",required = true),
            @ApiImplicitParam(name = "pageSize", value = "数量", dataType = "Integer", paramType = "query",required = true),
            @ApiImplicitParam(name = "roleId", value = "筛选, 2.爱心 3.学校 4.机构 5.父母", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "筛选,0.未审核  1.正常使用 -1.停用中", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "searchKey", value = "根据用户名搜索", dataType = "String", paramType = "query"),
    })
    @GetMapping("/userList")
    public ResponseResult userList(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                                 @RequestParam(required = false) String searchKey,
                                                 @RequestParam(required = false) Integer roleId,
                                                @RequestParam(required = false) Integer state){
        PageHelper.startPage(pageNum,pageSize);
        return ResponseResult.success(new PageInfo<>(userService.getUserList(roleId,state,searchKey)));
    }


    @RequiresRoles("admin")
    @ApiOperation("管理员获取用户信息")
    @GetMapping("/user/{userId}")
    public ResponseResult userList(@PathVariable("userId")  Integer userId){
        return ResponseResult.success(userService.getUserVOById(userId));
    }

    @RequiresRoles("admin")
    @ApiOperation("管理员获取所有学校")
    @GetMapping("/schoolAll")
    public ResponseResult schoolAll(){
        return ResponseResult.success(userService.getSchoolAll());
    }

    @RequiresRoles(value = {"admin","school"},logical = Logical.OR)
    @ApiOperation("管理员获取所有组织机构")
    @GetMapping("/organizationAll")
    public ResponseResult organizationAll(){
        return ResponseResult.success(userService.getOrganizationAll());
    }

    @RequiresRoles("admin")
    @ApiOperation("获取组织机构分页信息")
    @GetMapping("/organizationPage")
    public ResponseResult organizationPage(Page page){
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        return ResponseResult.success(new PageInfo<>(userService.getOrganizationPage(page.getSearchKey())));
    }

    @RequiresRoles("admin")
    @ApiOperation("获取学校分页信息")
    @GetMapping("/schoolPage")
    public ResponseResult schoolPage(Page page){
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        return ResponseResult.success(new PageInfo<>(userService.getSchoolPage(page.getSearchKey())));
    }


}
